<?php
    class UserAction extends Action{
    	function __construct(){
        	parent::__construct();
        	$map = array();
       	}

		public function search(){
			$user = D('SearchView');
			import('ORG.Util.Page');
			$count = $user->where('status=1')->count();
			$Page = new Page($count,20);
			$show  = $Page->show();
			$list = $user->where('status=1')->order('stu_id, grade, term, subject')->limit($Page->firstRow.','.$Page->listRows)->select();
			$this->assign('list',$list);
			$this->assign('page',$show);
            $this->display();

            //print_r(($data));
			
		}

		public function readid() {
			$user = D('SearchView');
			global $map;
			$name = $this->_param('name');
			$stu_id = $this->_param('stu_id');
			$score = $this->_param('score');
			$d = $this->_param('d');
			if ($_GET['p'] == 1) {
			if ($d) {
				$map['Score.score'] = array('gt', $score);
			} else {
				$map['Score.score'] = array('lt', $score);
			}
			$map['Student.name'] = array('like',"%$name%");
			$map['Student.stu_id'] = array('like', "%$stu_id%");
			}
			import("ORG.Util.Page");
        
        	$count = $user->where($map)->count();
        	$Page = new Page($count,20);

       		$show = $Page->show();
        
			$data = $user->where($map)->order('stu_id, score')->page($_GET['p'].',20')->select();

        	$this->assign('page',$show);
			$this->assign('list', $data);
			$this->display();
		}

		public function read() {
			$stu_id = $this->_get('stu_id');
			$user = D('SearchView');

			$grade = $this->_post('grade');
			$term = $this->_post('term');
			$subject = $this->_post('subject');

			if ($grade == 0) {
				$where['Student.stu_id'] = $stu_id;
			} elseif ($term == 0) {
				$where['Student.stu_id'] = $stu_id;
				$where['Score.grade'] = $grade;
			} elseif ($subject == 0) {
				$where['Score.term']= $term;
                $where['Student.stu_id'] = $stu_id;
                $where['Score.grade'] = $grade;

			} else {
				$where['Student.stu_id'] = $stu_id;
                $where['Score.term']= $term;
                $where['Student.stu_id'] = $stu_id;
                $where['Score.subject'] = $subject;
			}
			

			$data = $user->where($where)->order('grade, term, subject')->select();
			if (!$data) {
				$this->assign('err_data', "无该条件下的分数");
			}

			$this->assign('stu_id', $stu_id);
			$this->assign('list', $data);
			$this->display();


		}

		public function add() {
			$user = D('Score');

			$stu_id = $this->_get('stu_id');
			$grade = $this->_post('grade');
			$term = $this->_post('term');
			$subject = $this->_post('subject');
			$score = $this->_post('score');

			$user->create();
			
			$row = $user->table('stu2_student')->where("stu_id = $stu_id")->field('itemid, updatetime')->select();

			$itemid = $row[0]['itemid'];

			if (!($grade && $term && $subject && $score!=='')) {
				$this->assign('err_null', "年级，学期，科目，分数都不能为空");
				//echo "年级，学期，科目，分数都不能为空";
			} elseif (($row[0]['updatetime'] + $grade) != C('DATE')) {
				//$this->error("你不能添加".$grade."年级学生。"."时间不符合");
				if ($date = (C('DATE') - $row[0]['updatetime'])) {
					$this->assign('err_grade', "你不能添加".$grade."年级分数，"."时间不符合。 你只可以添加".$date."年级分数");
				} else {
					$this->assign('err_grade', "$stu_id 现在还不能添加分数");
				}

			} else {
				$s = $user->where("itemid = $itemid and grade = $grade and term = $term and subject = $subject")
					->field('score')->select();

				if ($s) {
					$this->assign('err_data', "分数已存在，不能添加，你可选择修改 <a href=__URL__/update/stu_id/$stu_id>修改</a>");
				} else {
	
				$data['itemid'] = $itemid;
				$data['grade'] = $grade;
				$data['term'] = $term;
				$data['subject'] = $subject;
				$data['score'] = $score;
				$data['createtime'] = time();

				$user->add($data);
				$this->success("添加成功");
	

			}
		}
			$this->assign('stu_id', $stu_id);
			$this->display();
		}

		public function update() {
			$stu_id = $this->_get('stu_id');
			$grade = $this->_post('grade');
			$term = $this->_post('term');
			$subject = $this->_post('subject');
			$score = $this->_post('score');

			$user = D('Score');
			$row = $user->table('stu2_student')->where("stu_id = $stu_id")->field('itemid, updatetime')->select();

			$itemid = $row[0]['itemid'];

			if (!($grade && $term && $subject && $score!=='')) {
				$this->assign('err_null', "年级，学期，科目，分数都不能为空");
				//echo "年级，学期，科目，分数都不能为空";
			} elseif (($row[0]['updatetime'] + $grade) != C('DATE')) {
				//$this->error("你不能添加".$grade."年级学生。"."时间不符合");
				if ($date = C('DATE') - $row[0]['updatetime']) {
					$this->assign('err_grade', "你不能修改".$grade."年级分数，"."时间不符合。 你只可以修改".$date."年级分数");
				} else {
					$this->assign('err_grade', "$stu_id 现在还不能修改分数");
				}

			} else {
				$s = $user->where("itemid = $itemid and grade = $grade and term = $term and subject = $subject")
					->field('score')->select();

				if (!$s) {
					$this->assign('err_data', "分数不存在，不能添加，你可选择添加 <a href=__URL__/add/stu_id/$stu_id>添加</a>");
				} else {
	
				$data['score'] = $score;
				$data['createtime'] = time();

				$user->where("itemid = $itemid and grade = $grade and term = $term and subject = $subject")
						->save($data);

				$this->success("添加成功");
	

			}
		}
			$this->assign('stu_id', $stu_id);
			$this->display();

		}
	}
    